package io.dcloud.test4050

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material3.Button
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class ComposeTextToView4050Activity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme {
                ComposeTextGridScreen()
            }
        }
    }
}

@Composable
fun ComposeTextGridScreen() {
    // 使用 state 来控制网格的显示和隐藏
    var showGrid by remember { mutableStateOf(false) }
    var startTime by remember { mutableStateOf(0L) }
    val context = LocalContext.current
    Column(modifier = Modifier.fillMaxSize()) {
        // 按钮行
        Row {
            Button(
                onClick = {
                    startTime = System.currentTimeMillis()
                    showGrid = true
                },
                modifier = Modifier.padding(8.dp)
            ) {
                Text("添加 View")
            }
        }

        // 条件性地显示网格
        if (showGrid) {
            val rowCount = 50
            val colCount = 40
            Log.e("test", "Compose start composition")

            // 当网格首次组合或重组时记录日志
            LaunchedEffect(Unit) {
                val compositionTime = System.currentTimeMillis() - startTime
                Log.e(
                    "test",
                    "Composition finished in: $compositionTime ms"
                )

                // 等待下一帧被绘制
                withFrameNanos { }

                val drawTime = System.currentTimeMillis() - startTime
                Log.e(
                    "test",
                    "First frame drawn in: $drawTime ms"
                )
                Toast.makeText(context, "总耗时: ${drawTime} ms", Toast.LENGTH_LONG).show()
            }
            TextGridView(rowCount = rowCount, colCount = colCount)
        }
    }
}

@Composable
fun TextGridView(rowCount: Int, colCount: Int) {
    // 使用 LazyColumn 和 LazyRow 来优化大规模列表的性能
    LazyColumn {
        items(rowCount) {
            LazyRow {
                items(colCount) { colIndex ->
                    // 单元格
                    Box(
                        modifier = Modifier
                            .padding(0.4.dp) // 模拟 margin
                            .background(Color(0xFF85D8B4))
                    ) {
                        // 内部文本视图
                        Text(
                            text = colIndex.toString(),
                            fontSize = 5.sp,
                            lineHeight = 5.sp,
                            style = LocalTextStyle.current.copy(
                                platformStyle = PlatformTextStyle(
                                    includeFontPadding = false
                                )
                            )
                        )
                    }
                }
            }
        }
    }
}